﻿using Microsoft.AspNetCore.Mvc.Filters;
using IdentityModel;
using Farming.Application.Traceabilities;
using Farming.Domain.Entities;
using Farming.Repositories;
namespace Farming.WebApi
{
    public class CustomActionFilterAsyncAttribute : IAsyncActionFilter
    {
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var res=await next();
            var name=res.HttpContext.User.FindFirst(JwtClaimTypes.Name)?.Value;
            if (name == null)
            {
                return;
            }
            var log = res.HttpContext.RequestServices.GetRequiredService<IRepository<AccesstInfo>>();
            AccesstInfo accesstInfo = new AccesstInfo()
            {
                AccesstTime = DateTime.Now,
                AccesstLogPath = res.HttpContext.Request.Path,
                AccesstName = name,
                AccesstLogType = "成功"
            };
            int n = await log.AddAsync(accesstInfo);

        }
    }
}
